package time;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author g1wshimu
 */
public class TimeUtil {

    private TimeUtil() {}
    
    public static float millisecondsToSeconds(long milliseconds) {
        return milliseconds / (float) 1000;
    }
    
    public static float millisecondsToMinutes(long milliseconds) {
        return millisecondsToSeconds(milliseconds) / (float) 60;
    }
    
    public static long minutesToMilliseconds(int minutes) {
        return minutes * 60 * secondsToMilliseconds(1);
    }

    public static long secondsToMilliseconds(int seconds) {
        return seconds * 1000;
    }

    public static Date convert(Date date, String timeZoneId) {
        Calendar calTZ = new GregorianCalendar(TimeZone.getTimeZone(timeZoneId));
        calTZ.setTimeInMillis(new Date().getTime());
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, calTZ.get(Calendar.YEAR));
        cal.set(Calendar.MONTH, calTZ.get(Calendar.MONTH));
        cal.set(Calendar.DAY_OF_MONTH, calTZ.get(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, calTZ.get(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, calTZ.get(Calendar.MINUTE));
        cal.set(Calendar.SECOND, calTZ.get(Calendar.SECOND));
        cal.set(Calendar.MILLISECOND, calTZ.get(Calendar.MILLISECOND));
        return calTZ.getTime();
    }
    
    public static String getTimeString(long millis) {
        return String.format("%02d:%02d:%02d ",
                TimeUnit.MILLISECONDS.toHours(millis),
                TimeUnit.MILLISECONDS.toMinutes(millis),
                TimeUnit.MILLISECONDS.toSeconds(millis)
                - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
    }
}
